/*
 * @Description:
 * @Author: cgt
 * @Date: 2021-12-23 14:05:56
 * @LastEditors: cgt
 * @LastEditTime: 2021-12-28 17:07:48
 */
import ITool from '../ITool';
import Layer from '../../constant/Layer';
import Prefix from '../../constant/Prefix';

/**
 * 标注控制工具箱
 * @implements {ITool}
 */
class LabelControlTool extends ITool {
  constructor(_top, _impl) {
    super(_top, _impl);
    this.name = 'LabelControlTool';
    this.desc = '标注控制工具箱';
    this.prefixList = Prefix.MarkerLabelList;
  }

  onAdd() {
    super.onAdd();
  }

  onRemove() {
    super.onRemove();
  }

  /**
   * 添加过滤前缀
   * @param {Array} list 前缀数组
   */
  addPrefix(list) {
    this.prefixList.push(...list);
  }

  /**
   * 切换标注显示状态
   * @param {Boolean} state 显示状态
   */
  changeTextState(state) {
    let layers = this.getControlLayersByIncludeTexts();
    // console.log(layers);
    let map = this.getMap();
    layers.forEach((layer) => {
      let textField = state ? Layer.GlobalMarkLabelField : Layer.GlobalMarkLabelField + 'hide';
      textField = '{' + textField + '}';
      if(map.getLayer(layer)) {
        map.setLayoutProperty(layer, 'text-field', textField);
      }
    });
  }

  /**
   * 根据标注前缀获取控制图层
   * @ignore
   */
  getControlLayersByIncludeTexts() {
    let layers = [];
    let totalLayers = Object.keys(this.getMap().style._layers);
    totalLayers.forEach((layerId) => {
      let metadata = this.getMap().getLayer(layerId).metadata;
      let enableLabelControl = metadata && metadata.enableLabelControl;
      if (this.includeText(layerId) || enableLabelControl) {
        layers.push(layerId);
      }
    });
    return layers;
  }

  /**
   * 图层Id是否含有指定前缀
   * @param {String} layerId
   */
  includeText(layerId) {
    return this.prefixList.some((text) => {
      return layerId.includes(text);
    });
  }
}

export default LabelControlTool;
